package day20;

import java.io.File;

/**
 * 递归列出目录下面的所有文件
 * 
 * @author wangbiao
 * 
 */
public class RecursionShowFile {

	public static void main(String[] args) {
		//recursionShowFile(new File("D:\\MyProject\\javatest\\src"));
		//System.out.println(getSum(10));
		recursionShowFile(new File("D:\\MyProject\\javatest\\src"),0);
	}

	//呈现文件夹之间的层次结构
	private static String getLevel(int level) {
		StringBuilder sb = new StringBuilder(16*2);
		sb.append("|--");
		for(int i = 0; i< level; i++) {
			sb.insert(0,"|  ");
		}
		return sb.toString();
	}
	
	public static void recursionShowFile(File dir,int level) {
		if (null == dir) {
			return;
		}
		//打印目录
		System.out.println(getLevel(level) +dir.getName());
		level ++;
		File[] files = dir.listFiles();
		for (int i = 0, len = files.length; i < len; i++) {
			if (files[i].isDirectory()) {
				recursionShowFile(files[i],level);
			} else {
				System.out.println(getLevel(level) + files[i]);
			}
		}
	}
	
	public static int getSum(int n) {
		if (n == 1) {
			return n;
		} else {
			return n + getSum(n-1);
		}
	}
	
	//递归打印目录中的文件
	public static void recursionShowFile(File dir) {
		//打印目录
		System.out.println(dir);
		if (null == dir) {
			return;
		}
		File[] files = dir.listFiles();
		for (int i = 0, len = files.length; i < len; i++) {
			if (files[i].isDirectory()) {
				recursionShowFile(files[i]);
			} else {
				System.out.println(files[i]);
			}
		}
	}

}
